<template>
    <div class="content">
        <div style="text-align:right;height:58px;">
            <Button type="primary" @click="handleAdd">新建</Button>
        </div>
        <Table :data="list" :columns="columns" border align="center">
            <template slot-scope="{ row,index }" slot="sort">
                <Input v-if="index==nowEdit" v-model="row.sort" />
                <div v-else>{{ row.sort }}</div>
            </template>
            <template slot-scope="{ row,index }" slot="sequence">
                <Input v-if="index==nowEdit" v-model="row.sequence" />
                <div v-else>{{ row.sequence }}</div>
            </template>
            <template slot="action" slot-scope="{ row,index }">
                <div class="table-action">
                    <Button v-if="index==nowEdit" type="text" @click="handleSave(row,index)">保存</Button>
                    <Button v-else type="text" @click="handleEdit(row,index)">编辑</Button>
                    <Button type="text" @click="handleDelete(row,index)">删除</Button>
                </div>
            </template>
        </Table>
        <Pagination
            :pageSize.sync="params.pageSize"
            :current.sync="params.pageNum"
            :total="params.total"
            @change="queryList"
        />
        <Modal
            v-model="addM"
            title="新建试题分类"
            @on-ok="ok"
            @on-cancel="cancel"
        >
            <Form ref="form" class="f-fl" :model="one" :rules="ruleValidate" inline :label-width="5">
                <FormItem prop="sort">
                    <Input
                        v-model="one.sort"
                        placeholder="分类名称"
                    ></Input>
                </FormItem>
                <FormItem prop="sequence">
                    <InputNumber
                        v-model="one.sequence"
                        placeholder="排序"
                    ></InputNumber>
                </FormItem>

            </Form>
            <div slot="footer">
                <Button type="text" @click="addM=false">取消</Button>
                <Button type="primary" @click="ok">确定</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
import { getSort, editSort, delSort } from '@/api/questionManage'
export default {
    data () {
        /* const validateSq = (rule, value, callback) => {
            if (value.length < 2 || value.length > 15 || !value.test(/^([0-9a-zA-Z]+[\.\-_])*[0-9a-zA-Z]+$/)) {
                callback(new Error('2-15，不重复'))
            } else {
                callback()
            }
        } */
        return {

            ruleValidate: {
                sort: [
                    { required: true, message: '必填不为空', trigger: 'blur' },
                    {
                        min: 3,
                        max: 15,
                        trigger: 'blur',
                        message: '3-15位'
                    }
                ],
                sequence: [
                    { type: 'number', required: true, message: '必填不为空', trigger: 'blur' }

                ]
            },
            one: {
                sort: '',
                sequence: 1
            },
            nrow: {
                sort: '',
                sequence: 1
            },
            addM: false,
            nowEdit: 'hh',
            list: [],
            params: {
                total: 0,
                pageNum: 1,
                pageSize: 10
            },
            columns: [
                { title: '序号', type: 'index', align: 'center', width: 80 },
                { title: '分类名称', slot: 'sort', align: 'center' },
                { title: '排序', slot: 'sequence', align: 'center' },
                { title: '创建时间', key: 'createTime', align: 'center' },
                { title: '操作', slot: 'action', align: 'center', width: 100 }
            ]
        }
    },
    mounted () {
        this.queryList()
    },
    methods: {
        handleAdd () {
            this.one.sequence = 1
            this.one.sort = ''
            this.addM = true
        },
        ok () {
            this.$refs.form.validate(v => {
                if (v) {
                    let params = {
                        sort: this.one.sort,
                        sequence: this.one.sequence
                    // schoolId:''
                    }

                    editSort(params).then(res => {
                        if (res.data.code != '0000') {
                            this.$Message.error(res.data.data)
                            return
                        }
                        this.$Message.success('成功')
                        console.log(res.data)
                        this.addM = false
                    })
                }
            })
        },
        cancel () {
            // this.addM = false;
        },
        queryList () {
            let params = {
                pageNum: this.params.pageNum,
                pageSize: this.params.pageSize,
                schoolId: '',
                sequence: '',
                sort: '',
                sortId: ''
            }
            getSort(params).then(res => {
                console.log(res)
                this.list = res.data.data.list
                this.params.pageNum = res.data.data.pageNum
                this.params.pageSize = res.data.data.pageSize
                this.params.total = res.data.data.total
            })
        },
        handleEdit (row, index) {
            this.nowEdit = index
            console.log(index)
        },
        handleDelete (row) {
            this.$Modal.confirm({
                title: '提示',
                content: '确认是否删除？',
                onOk: () => {
                    let params = {
                        sortId: row.sortId
                    }
                    delSort(params).then(res => {
                        if (res.data.code != '0000') {
                            this.$Message.error(res.data.data)
                        } else {
                            this.$Message.success('删除成功！')
                        }
                    })
                }
            })
        },
        handleSave (row) {
            if (row.sort && row.sequence) {
                let params = {
                    sort: row.sort,
                    sequence: row.sequence,
                    // schoolId:''
                    sortId: row.sortId
                }

                editSort(params).then(res => {
                    if (res.data.code != '0000') {
                        this.$Message.error(res.data.data)
                        return
                    }
                    this.$Message.success('成功')
                    console.log(res.data)
                    this.nowEdit = 'hh'
                })
            } else {
                this.$Message.info('请填写完整')
            }
        }
    }
}
</script>
